#import ImageMetrics as IM
import Metric
import pylab
import Image
from numpy import zeros, mgrid, ones
import unittest

import JT
import Calibration

class Test(unittest.TestCase):
    def runTest(self):
#===============================================================================
#    Data Paths
#===============================================================================
        
        fixedImageName = 'C:/Users/bryan/bryan-code/2D3D/vert1/fluoro/ushortim080-LAT.mhd'
        #fixedImageName = 'C:/Users/bryan/bryan-code/trunk/Images/CalibratedDRRImage.mhd'
        fixedImageMaskName = 'C:/Users/bryan/bryan-code/2D3D/vert1/fluoro/ucharb2Rmask080-LAT.mha'
        
        inputVolFileName = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1.mha'
        maskFileName = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1b2VOI.mha'
        staFile = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1.sta'
        calFileLat = 'C:/Users/bryan/bryan-code/trunk/TestData/ext_cal1.txt'
        
        debugFolder = 'C:/Users/bryan/bryan-code/trunk/Images/debug/'
        
        
#===============================================================================
#    Setup DRR
#===============================================================================
        
        xraycam = JT.XRayCamera()
        drr = JT.DRR()
        drr.SetXRayCamera(xraycam)
        drr.SetVolumeFilename(inputVolFileName)
        drr.SetBlackOnWhiteImage()
        drr.InteractionOff()
        
        cal = Calibration.ExternalCalibration()
        cal.LoadConsolidatedCalibration(calFileLat)
        cal.LoadStaFile(staFile)
        
        xraycam.SetExternalCalibration(cal)
        
        drr._volume.SetOriginalTransform(cal._VolumeT)
        drr._volume.SetBodyCenter((3.906, 2.17, -7.378))
        #drr._volume.UseRayCastMapper()
        
        # Set Fixed image using path
        fixedImage = JT.FixedImage()
        fixedImage.SetFileName(fixedImageName)
        
        reg=JT.Registration()
        reg.SetFixedImage(fixedImage.GetImage(0))
        reg.SetMovingImageGenerator(drr)
        reg.SetImageMetric('mi')
        #reg._blur = 2.0
        
        #Set Mask (choose one, moving or fixed)
        #drr._volume.SetImageMask(maskFileName)
        #reg.SetFixedImageMask(fixedImageMaskName)
        
        #Based on the fluoro mask, i determined the following region of interest 
        # (row,column) format for numpy array:
        roi = [[210,187],[277,370]]
        reg.SetRegionOfInterest(roi)
        
        poses = JT.Testing()
        val = ones(200)
        pose = poses.GetStartPose(0)
        fillVal = reg.GetValue(pose)
        val *= fillVal
        pylab.plot(val,hold=False)
        pylab.title(reg._metricParams['method'])
        pylab.xlabel('Start Position')
        pylab.ylabel('Metric Value')
        print 'roi: ', reg._metricParams['roi']

        for i in range(200):
        #            pylab.imshow(currentImage)
            pylab.figure(1)
            pose = poses.GetStartPose(i)
            val[i] = reg.GetValue(pose)
            pylab.plot(val,hold=False)
            print "Iteration: %i, Metric Value: %f" % (i,val[i])
            #GDMetric.SaveImage(image, filename)
        
        return val


if __name__ == "__main__":
    test=Test()
    val = test.runTest()